package com.tcy.netlogin;

import org.apache.http.client.HttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class NetLoginActivity extends Activity {
    /** Called when the activity is first created. */
	private DatabaseOpera db = null;
	private EditText v_username = null;
	private EditText v_password = null;
	private EditText v_loginURL = null;
	private EditText v_logoutURL = null;
	private CheckBox cb_autoLogin = null;
	private CheckBox cb_autoLogout = null;
	private Button btn_save = null;
	private Button btn_login = null;
	private Button btn_logout = null;
	private TextView v_status = null;
	public Info data = null;
	public HttpClient httpclient = new MyDefaultHttpClient();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        db = new DatabaseOpera(this.getApplicationContext());
        
        v_status = (TextView)this.findViewById(R.id.status);
        
        //v_status.setCursorVisible(false);      
        //v_status.setFocusable(false);         
        //v_status.setFocusableInTouchMode(false);  
        
        v_username = (EditText) this.findViewById(R.id.username);
        v_password = (EditText) this.findViewById(R.id.password);
        v_loginURL = (EditText) this.findViewById(R.id.loginURL);
        v_logoutURL = (EditText) this.findViewById(R.id.logoutURL);
        
        cb_autoLogin = (CheckBox) this.findViewById(R.id.autoLogin);
        cb_autoLogout = (CheckBox) this.findViewById(R.id.autoLogout);
        btn_save = (Button) this.findViewById(R.id.savebtn);
        btn_login = (Button)this.findViewById(R.id.loginbtn);
        btn_logout = (Button)this.findViewById(R.id.logoutbtn);
        
        
        data = db.query();
        if(data != null)
        {
        	v_username.setText(data.username);
        	v_password.setText(data.password);
        	v_loginURL.setText(data.loginURL);
        	v_logoutURL.setText(data.logoutURL);
        	boolean autoLogin = data.autoLogin.equals("true") ? true : false;
        	cb_autoLogin.setChecked(autoLogin);
        	boolean autoLogout = data.autoLogout.equals("true") ? true : false;
        	cb_autoLogout.setChecked(autoLogout);
        }
        
        btn_save.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		data.username = v_username.getText().toString();
        		data.password = v_password.getText().toString();
        		data.loginURL = v_loginURL.getText().toString();
        		data.logoutURL = v_logoutURL.getText().toString();
        		data.autoLogin = ""+cb_autoLogin.isChecked();
        		data.autoLogout = ""+cb_autoLogout.isChecked();
        		//Log.i("dd", data.autoLogin);
        		db.insert(data);
        	}
        });
        
        btn_login.setOnClickListener(new LoginBtnListener(this));
        btn_logout.setOnClickListener(new LogoutBtnListener(this));
        
        int status = getStatus();
        if(data.autoLogin.equals("true") && status == Status.OFF)
        {
        	//System.out.println("login");
        	login();
        }else if(data.autoLogout.equals("true") && status == Status.ON)
        {
        	//System.out.println("logout");
        	logout();
        }else
        {
        	
        }
    }
    
    public int getStatus()
    {
    	try
    	{
			Thread t = new Thread(new NetThread(this, NetThread.GETSTATUS));
			t.start();
			t.join();
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	v_status.setText(Status.msg);
    	return Status.code;
    }
    
    public void login()
    {
    	try
    	{
			Thread t = new Thread(new NetThread(this, NetThread.LOGIN));
			t.start();
			t.join();
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	v_status.setText(Status.msg);
    }
    
    public void logout()
    {
    	try
    	{
			Thread t = new Thread(new NetThread(this, NetThread.LOGOUT));
			t.start();
			t.join();
    	}catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	v_status.setText(Status.msg);
    }
}